<?php
/**
 * @Author: rocky
 * Date: 2019/9/29
 * Time: 16:38
 */


namespace app\shop\controller\api\v1;


use app\common\controller\BaseApiController;
use app\common\facade\Token;
use app\shop\model\ShopCollect;
use app\shop\resource\CollectResoure;

/**
 * 商品收藏
 * Class Collect
 * @package app\shop\controller\api\v1
 */
class Collect extends BaseApiController
{
    //我的收藏商品
    public function getInfo(){
        $data = ShopCollect::uid()->where('is_collect',1)->pages()->with(['good'=>function($q){
            $q->withSum('specs','sale');
        }])->select();
        $this->successCode(CollectResoure::collection($data));
    }
    //收藏/取消
    public function putInfo(){
        $good_id = $this->request->param('good_id');
        $collect = ShopCollect::uid()->where('good_id',$good_id)->find();
        if($collect){
            if($collect['is_collect'] == 1){
                $collect->is_collect = 0;
            }else{
                $collect->is_collect = 1;
            }
            $collect->save();
        }else{
            ShopCollect::create([
                'uid'=>Token::id(),
                'good_id'=>$good_id,
            ]);
        }
        $this->successCode();
    }
}